package com.zhiyou100.review.day021.homework.homework4;

import com.zhiyou100.review.day021.homework.homework3.UploadFile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @packageName: DownloadFile
 * @className: com.zhiyou100.review.day021.homework.homework4
 * @Description: TODO UDP 服务器
 * @author: yang
 * @date: 7/2/20
 */
public class DownloadFile {
    private static final int PORT = 10010;
    private static final String IP = "127.0.0.1";
    private static final int SERVER_PORT = 10086;
    private static final String PATH = "/Users/yang/Downloads/test/accept";

    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(SERVER_PORT);
        // 创建 socket
        BufferedWriter digit = new BufferedWriter(new FileWriter(new File(PATH, "digit.txt")));
        BufferedWriter letter = new BufferedWriter(new FileWriter(new File(PATH, "letter.txt")));
        BufferedWriter other = new BufferedWriter(new FileWriter(new File(PATH, "other.txt")));
        // 创建 3个 高效流 写入文件

        byte[] bytes = new byte[1073741824];
        // 1073741824 1G
        DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);

        socket.receive(datagramPacket);

        int length = datagramPacket.getLength();
        String s = new String(bytes, 0, length);
        // 接受的 文件的 String

        digit.write(com.zhiyou100.review.day021.homework.homework3.UploadFile.getDigest(s));
        digit.flush();
        letter.write(com.zhiyou100.review.day021.homework.homework3.UploadFile.getLetter(s));
        letter.flush();
        other.write(UploadFile.getOther(s));
        other.flush();
        // 分别写入三个文件，并刷新


        socket.close();
        // 关闭
    }
}
